package com.baizhi.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * 练习：定义一条线程，用于计算1-n之和
 */
public class ThreadTest01 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        GetNumSum gns = new GetNumSum(900);
        FutureTask ft = new FutureTask(gns);
        Thread t1 = new Thread(ft);
        t1.start();

        String str = (String)ft.get();
        System.out.println(str);
    }
}


class GetNumSum implements Callable<String> {

    private int n;

    public GetNumSum(int n) {
        this.n = n;
    }

    @Override
    public String call() throws Exception {
        int sum = 0;
        for (int i = 1; i <= n; i++) {
            sum += i;
        }
        return "1到"+n + "之和是：" + sum;
    }
}


